package com.gopay.cron.event.handler;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.gopay.common.cps.dao.trans.CpsBatchTaskTargetRelDAO;
import com.gopay.common.datasource.DBType;
import com.gopay.common.datasource.DbContextHolder;
import com.gopay.common.domain.cps.CpsBatchTaskTargetRel;
import com.gopay.common.domain.report.Event;
import com.gopay.common.event.EventState;
import com.gopay.common.event.ReportEventType;
import com.gopay.common.event.handler.EventHandler;
import com.gopay.common.exception.GopayException;
import com.gopay.common.report.dao.MqEventInfoDAO;
import com.gopay.cron.event.manager.EventHandlerManager;
import com.gopay.cron.service.trans.ReportConsoleTransQueryService;
import com.gopay.scheduler.job.vo.EventVo;

@Service(value = "batchPayTaskCreateEventDefaultHandler")
public class BatchPayTaskCreateEventDefaultHandler implements EventHandler {

    @Resource(name = "cpsBatchTaskTargetRelDAO4Event")
    private CpsBatchTaskTargetRelDAO cpsBatchTaskTargetRelDAO;

    @Resource
    private ReportConsoleTransQueryService reportConsoleTransQueryService;
    @Resource(name = "createtEventHandlerManager")
    private EventHandlerManager CreatetEventHandlerManager;
    @Resource(name = "mqEventInfoDAO")
    private MqEventInfoDAO mqEventInfoDAO;

    @Override
    @Transactional
    public void handler(Event e) throws GopayException {
        String batchTaskId = e.getBatchTaskId();
        List<CpsBatchTaskTargetRel> taskList = cpsBatchTaskTargetRelDAO.findByTaskId(Long.valueOf(batchTaskId));
        Set<EventVo> evoSet = new HashSet<EventVo>();
        List<Event> eventList = new ArrayList<Event>(); 
        Map<String, String> map = new HashMap<String, String>();
       
        for (CpsBatchTaskTargetRel c : taskList) {
            EventVo evo_ = new EventVo();
            evo_.setGopayOrderId(c.getBatchTargetId());
            evo_.setType(ReportEventType.ORDER_SAVE.value);
            if(evoSet.add(evo_)){
                Event event = new Event();
                event.setType(evo_.getType());
                String gopayOrderId = evo_.getGopayOrderId();
                event.setGopayOrderId(gopayOrderId);
                Date currentDate = new Date();
                event.setCreatedTs(currentDate);
                event.setLastUpdTs(currentDate);
                event.setStat(EventState.PROGRESS.value);
                event.setSnapshot(JSONObject.toJSONString(map));
                event.setVersion(0);
                map.put("gopayOrderId", gopayOrderId);
                eventList.add(event);
            }
        }
        
        DbContextHolder.setDbType(DBType.DS_REPORT.value);
//        mqEventInfoDAO.batchSave(eventList);
        for (Event e1 : eventList) {
            mqEventInfoDAO.save(e1);
        }
        e.setStat(EventState.SUCCESS.value);
        mqEventInfoDAO.update(e);
    }

}
